package com.zyh.collection;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * @Author zyh
 * @Date 2024/7/30
 * @Description ArrayList方法
 */
public class CollectionDemo2 {
    public static void main(String[] args) {
        List list=new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println("list = " + list);//list = [1, 2, 3, 4]
        //根据索引在指定位置添加元素
        list.add(1,0);
        System.out.println("list = " + list);//list = [1, 0, 2, 3, 4]
        //根据索引删除指定位置元素
        Object remove = list.remove(4);
        System.out.println("remove = " + remove);//remove = 4
        System.out.println("list = " + list);//list = [1, 0, 2, 3]
        //根据指定索引修改对应位置元素
        list.set(1,100);
        System.out.println("list = " + list);//list = [1, 100, 2, 3]
        //查询
        Object o = list.get(3);
        System.out.println("o = " + o);//o = 3

        System.out.println("-------------------------");
        int index1 = list.indexOf(100);
        System.out.println("index1 = " + index1);
        int index2 = list.lastIndexOf(3);
        System.out.println("index2 = " + index2);
        //截取
        List list1 = list.subList(1,4);
        System.out.println("list1 = " + list1);
        System.out.println("--------------------------");
        //遍历
        list.forEach(item-> System.out.println(item));
        System.out.println("-----------------------------");
        //迭代器对象遍历
        ListIterator listIterator = list1.listIterator();
        while (listIterator.hasNext()){
            System.out.println(listIterator.next());
        }
    }
}
